//扫描所有的数据集，并且将这些数据集中的小粒度顺序部分调出来
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>

#define TRACE_NUM 36

#define MAX_LINE_SIZE 2048

int main(){
    //用一个数来记录一共遍历了多少数据
    long record_count = 0;
    //这里记录一共获取了多少数据
    long get_record = 0;


    //创建一个数组，MSR数据集中的所有文件
    char* trace_arr[TRACE_NUM] = {"resource/hm_0.csv", "resource/hm_1.csv",
        "resource/mds_0.csv", "resource/mds_1.csv",
        "resource/prn_0.csv", "resource/prn_1.csv",
        "resource/proj_0.csv", "resource/proj_1.csv", "resource/proj_2.csv", "resource/proj_3.csv", "resource/proj_4.csv",
        "resource/prxy_0.csv", "resource/prxy_1.csv",
        "resource/rsrch_0.csv", "resource/rsrch_1.csv", "resource/rsrch_2.csv", "resource/src1_0.csv", "resource/src1_1.csv",
        "resource/src1_2.csv", "resource/src2_0.csv", "resource/src2_1.csv", "resource/src2_2.csv", "resource/stg_0.csv",
        "resource/stg_1.csv", "resource/ts_0.csv", "resource/usr_0.csv", "resource/usr_1.csv", "resource/usr_2.csv",
        "resource/wdev_0.csv", "resource/wdev_1.csv", "resource/wdev_2.csv", "resource/wdev_3.csv", "resource/web_0.csv",
        "resource/web_1.csv", "resource/web_2.csv", "resource/web_3.csv"};

    //创造一个新的文件来写入
    FILE* fp_write = NULL;
    FILE* fp_read = NULL;

    fp_write = fopen("./resource/fix-tracecsv-20180628","w+");

    //用一个变量记录上一次访问的节点
    long last_offset = -1;
    long last_size = -1;

    //记录上一次写入新数据集的记录
    long last_insert_offset = -1;
    long last_insert_size = -1;
    
    char line[MAX_LINE_SIZE];

    int i;

    for(i = 0; i < TRACE_NUM; i++){
        if((fp_read = fopen(trace_arr[i], "r")) != NULL){
            printf("开始读读取数据集：%s\n", trace_arr[i]);
            
            char delims[] = ",";
            char *result = NULL;

            long offset = -1;
            long size = -1;

            int i = 0;

            while (fgets(line, MAX_LINE_SIZE, fp_read)){            
                //这里是个分割的元素
                result = strtok( line, delims );

                // printf("%s", result);

                //这里要进行计数，只保留第5列
                i = 1;

                //查看当前是不是要处理的
                int judge = 0;

                //只要还有可以分的，那result不会是NULL
                while((result = strtok( NULL, delims))){
                    i++;
                    
                    //这里将offset也考虑在内
                    if(i == 4){
                        //这里我们看看是不是读，如果是读，那么就意味着真的可以看看历史的访问记录，并且将顺序的部分写到文件中
                        //每一次插入都是将当前访问的节点和上次访问节点的在之前没有插入的情况下
                        
                        //看看是不是连续的
                        //只关注读
                        
                        if(result[0] == 'R'){
                            record_count++;
                            judge = 1;
                        }
                    }

                    if(i == 5){
                        offset = atol(result);
                    }

                    if(i == 6){
                        size = atol(result);
                        
                        if(judge == 1){
                            // printf("查找到读记录,%ld,%ld\n", offset, size);
                            if(offset <= last_offset + last_size + 1 && offset >= last_offset + last_size - 1){
                                //这里说明就是连续的，进行写入
                                //记录一下获取的记录数量
                                get_record++;
                                
                                //先写入上一次的
                                //如果之前没有插入过，那就进行插入
                                if(!(last_offset == last_insert_offset && last_size == last_insert_size)){
                                    //这里进行插入
                                    fprintf(fp_write, "%ld,%ld\n", last_offset, last_size);
                                    last_insert_offset = last_offset;
                                    last_insert_size = last_size;
                                }

                                //插入这一次的
                                if(!(offset == last_insert_offset && size == last_insert_size)){
                                    //插入当前节点
                                    fprintf(fp_write, "%ld,%ld\n", offset, size);

                                    last_insert_offset = offset;
                                    last_insert_size = size;
                                }
                            }

                            //更新上一次访问的节点
                            last_size = size;
                            last_offset = offset;
                        }
                    }
                }
            }

            fclose(fp_read);
            fp_read = NULL;
            printf("遍历总数据量：%ld\n", record_count);
            printf("获取总数据量：%ld\n", get_record);
        }else{
            printf("文件开启失败\n");
        }
    }

    fclose(fp_write);
}